package com.hyz.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.hyz.pojo.Asset;
import com.hyz.pojo.RepositoryAsset;
import com.hyz.service.AssetService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;

/**
 * @author HGD
 * @date 2022/10/4 14:35
 */
@Component
public class AssetListener {
    @Autowired
    private AssetService service;

    /**
     * 批量修改
     *
     * @param message json字符串
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "asset.queue"),
            exchange = @Exchange(name = "repositoryManager", type = ExchangeTypes.DIRECT),
            key = {"batchUpdateAssets"}
    ))
    public void batchUpdateAssets(String message) {
        ArrayList<Asset> assets = JSON.parseObject(message, new TypeReference<ArrayList<Asset>>() {
        });
        service.updateBatchById(assets);
    }
}
